import 'package:built_collection/built_collection.dart';
import 'package:equatable/equatable.dart';
import 'package:mobile_app.core/core/domain/entities/learning_content/collection_list_item/collection_list_item.dart';
import 'package:mobile_app.core/core/domain/entities/learning_content/lexical_item/lexical_item.dart';
import 'package:mobile_app.core/core/domain/services/image_service/service.dart';
import 'package:mobile_app.home.menu.settings/image_settings_page/domain/helpers/get_all_image_cache_ratio/helper.dart';
import 'package:mobile_app.home.menu.settings/image_settings_page/domain/helpers/get_all_image_download_task_id/helper.dart';
import 'package:mobile_app.home.menu.settings/image_settings_page/domain/helpers/get_collection_ids/helper.dart';
import 'package:mobile_app.home.menu.settings/image_settings_page/domain/helpers/get_collection_image_cache_ratio_map/helper.dart';
import 'package:mobile_app.home.menu.settings/image_settings_page/domain/helpers/get_image_downloading_collection_ids/helper.dart';

part 'result.dart';

abstract class GetInitialData {
  InitialDataGettingResult call({
    required BuiltList<CollectionListItem> collectionListItems,
    required BuiltMap<String, LexicalItem> lexicalItemMap,
  });
}

class GetInitialDataImpl implements GetInitialData {
  const GetInitialDataImpl({
    required GetCollectionIds getCollectionIds,
    required GetImageDownloadingCollectionIds getImageDownloadingCollectionIds,
    required GetAllImageCacheRatio getAllImageCacheRatio,
    required GetAllImageDownloadTaskId getAllImageDownloadTaskId,
    required GetCollectionImageCacheRatioMap getCollectionImageCacheRatioMap,
    required ImageService imageService,
  })  : _getCollectionIds = getCollectionIds,
        _getImageDownloadingCollectionIds = getImageDownloadingCollectionIds,
        _getAllImageCacheRatio = getAllImageCacheRatio,
        _getAllImageDownloadTaskId = getAllImageDownloadTaskId,
        _getCollectionImageCacheRatioMap = getCollectionImageCacheRatioMap,
        _imageService = imageService;

  final GetCollectionIds _getCollectionIds;
  final GetImageDownloadingCollectionIds _getImageDownloadingCollectionIds;
  final GetAllImageCacheRatio _getAllImageCacheRatio;
  final GetAllImageDownloadTaskId _getAllImageDownloadTaskId;
  final GetCollectionImageCacheRatioMap _getCollectionImageCacheRatioMap;

  final ImageService _imageService;

  @override
  InitialDataGettingResult call({
    required BuiltList<CollectionListItem> collectionListItems,
    required BuiltMap<String, LexicalItem> lexicalItemMap,
  }) {
    final collectionIds = _getCollectionIds(
      collectionListItems: collectionListItems,
    );

    final imageDownloadingCollectionIds = _getImageDownloadingCollectionIds(
      collectionIds: collectionIds,
      downloadTaskIds: _imageService.downloadTaskIds,
    );

    final cacheSizeInBytes = _imageService.cacheSizeInBytes;

    final cachedImageIds = _imageService.cachedImagePathMap.keys.toBuiltSet();

    final allImageCacheRatio = _getAllImageCacheRatio(
      lexicalItemMap: lexicalItemMap,
      cachedImageIds: cachedImageIds,
    );

    final collectionImageCacheRatioMap = _getCollectionImageCacheRatioMap(
      collectionListItems: collectionListItems,
      lexicalItemMap: lexicalItemMap,
      cachedImageIds: cachedImageIds,
    );

    final allImageDownloadTaskId = _getAllImageDownloadTaskId();

    final allImagesDownloading = _imageService.downloadTaskIds.contains(allImageDownloadTaskId);

    final result = InitialDataGettingResult(
      imageDownloadingCollectionIds: imageDownloadingCollectionIds,
      collectionImageCacheRatioMap: collectionImageCacheRatioMap,
      allImagesDownloading: allImagesDownloading,
      allImageCacheRatio: allImageCacheRatio,
      cacheSizeInBytes: cacheSizeInBytes,
    );

    return result;
  }
}
